package th;

import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author Mark
 * @version 1.0
 * @date 2020/5/24 11:31
 * @description 线程不安全解决
 */
public class ThreadJUC {

    public static void main(String[] args) {
        CopyOnWriteArrayList list = new CopyOnWriteArrayList();
        for (int i = 0; i < 10000; i++) {
            new Thread(() -> {
                // volatile关键字的作用
                list.add(Thread.currentThread().getName());
            }).start();
        }
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(list.size());
    }
}
